package com.sgb.kjwl.viewmodel.machine;

import com.sgb.kjwl.api.APIManager;
import com.swgk.core.base.api.ICallBack;
import com.sgb.jicai.entity.CentralizedMiningEntity;
import com.sgb.jicai.entity.JiCaiHomeListRequestBean;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.kjwl.repository.MachineRepertory;
import com.sgb.kjwl.utils.AddressHelper;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.sgb.kjwl.view.ui.fragment.machine.AskLeasePickFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;
import com.swgk.core.view.selection.CommonPopEntity;

import java.util.List;

public class AskLeasePickFragmentModel extends BaseViewModel {

    private final AskLeasePickFragment fragment;
    private final MachineRepertory repertory;
    private int page = 1;

    public AskLeasePickFragmentModel(AskLeasePickFragment fragment, MachineRepertory repertory) {
        this.fragment = fragment;
        this.repertory = repertory;
    }
    public String getAreaNameModel() {
        return repertory.getAreaName();
    }

    public String getAreaCodeModel() {
        return repertory.getAreaCode();
    }

    /**
     * 需方发起列表
     */
    public void getJiCaiList(JiCaiHomeListRequestBean map, boolean isLoadMore) {
        DialogHelper.showProgressDialog(fragment.getActivity(), "加载中...").setCanceledOnTouchOutside(false);
        page = isLoadMore ? page + 1 : 1;
        map.setCurrent(page);
        map.setLimit(10);
        APIManager.getInstance().getCentralizedMiningAPI().getJiCaiLists(map).enqueue(new ICallBack<BaseEntity<ListResponse<List<CentralizedMiningEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<CentralizedMiningEntity>>> data) {
               DialogHelper.dismissProgressDialog();
                if (data == null || data.getState() == null) {
                    return;
                }
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.setJiCaiList(data.getData().getList(), isLoadMore);
                } else {
                    MToast.showToast(fragment.getContext(), data.getMsg());
                }
            }
        });
    }

    public void getH5Url(String id) {
        DialogHelper.showProgressDialog(fragment.getActivity(), "加载中...").setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().getUrlSetting("collection").enqueue(new ICallBack<BaseEntity<H5ReEntity>>() {
            @Override
            public void onSuccess(BaseEntity<H5ReEntity> data) {
              DialogHelper.dismissProgressDialog();
                if (data == null || data.getState() == null) {
                    return;
                }
                if (data.getState().equals(APIManager.RESPONSE_OK)) {//type 0 首页 1 我的集采 2 我的报价
                    SharedPreferenceUtil.init(fragment.getActivity());
                    String token = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.resourceAPPToken, "");
                    String url = data.getData().getUrl() + "?id=" + id + "&type=0" + "&token=" + token;
                    WebViewX5HostActivity.start(fragment.getActivity(), url);
                } else {
                    MToast.showToast(fragment.getActivity(), data.getMsg());
                }
            }
        });
    }

    //集采状态
    public void groupBuyClassList() {
        DialogHelper.showProgressDialog(fragment.getActivity(), "加载中...").setCanceledOnTouchOutside(false);
        APIManager.getInstance().getCentralizedMiningAPI().getStatus().enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getData() != null && data.getData() != null && data.getData().size() > 0) {
                    data.getData().get(0).setNo(""); // 全部的key值为-1，
                    fragment.showStateData(data.getData());
                }
            }
        });
    }

//
//    public void getDataList(String categoryType, String classNo, String typeNo,String provinceNo, String cityNo, String addrNo, String sortNo, int page) {
//
//        if ("".equals(categoryType)) {
//            LeaseAskFilterJson filterJson = new LeaseAskFilterJson();
//            filterJson.setProvinceNo(provinceNo);
//            filterJson.setCityNo(cityNo);
//            filterJson.setCurrent(page);
//            filterJson.setSortWay(sortNo);
//            if (!TextUtils.isEmpty(addrNo)) {
//                List<String> addrNos = Arrays.asList(addrNo.split(","));
//                filterJson.setAreaNos(addrNos);
//            }
//            getAskRentLeaseAll(filterJson);
//            return;
//        }
//
//        Map map = new HashMap();
//        map.put("areacode", provinceNo);
//        map.put("rentalTitle", "");
//        map.put("PageNum", page);
//        map.put("pageSize", 10);
//        map.put("categoryType", categoryType);//租赁类目(0:机械;1:设备;2:车辆;3:房产;)
//        map.put("categoryParentId", classNo);//分类ID
//        map.put("categoryChildId", typeNo);//类型ID
//        map.put("rentAddressProvinceId", provinceNo);//区域编号
//        map.put("cityId", cityNo);//市ID
//        map.put("sortWay", sortNo);//排序
//        if (!TextUtils.isEmpty(addrNo)) {
//            map.put("areaId", addrNo);//区,多选
//        }
//
//        APIManager.getInstance().getMachineAPI().getEquList(map).enqueue(new Callback<BaseEntity<ProListEntity<List<AskLeaseSheBeiEntity>>>>() {
//            @Override
//            public void onResponse(Call<BaseEntity<ProListEntity<List<AskLeaseSheBeiEntity>>>> call, Response<BaseEntity<ProListEntity<List<AskLeaseSheBeiEntity>>>> response) {
//                if (response.body() == null || response.body().getState() == null) {
//                    return;
//                }
//
//                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
//                    if (null != response.body().getData()) {
//                        fragment.showList(response.body().getData().getList());
//                    } else {
//                        MToast.showToast(fragment.getActivity(), "暂无数据");
//                    }
//                }
//            }
//
//            @Override
//            public void onFailure(Call<BaseEntity<ProListEntity<List<AskLeaseSheBeiEntity>>>> call, Throwable t) {
//                Log.v("dd", "sdsdd");
//            }
//        });
//    }
//
//    public void getAskRentLeaseAll(LeaseAskFilterJson leaseAskFilterJson) {
//        APIManager.getInstance().getMachineAPI().getAskRentLeaseAll(leaseAskFilterJson).enqueue(new Callback<BaseEntity<ProListEntity<List<AskLeaseSheBeiEntity>>>>() {
//            @Override
//            public void onResponse(Call<BaseEntity<ProListEntity<List<AskLeaseSheBeiEntity>>>> call, Response<BaseEntity<ProListEntity<List<AskLeaseSheBeiEntity>>>> response) {
//                if (response.body() == null || response.body().getState() == null) {
//                    return;
//                }
//
//                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
//                    if (null != response.body().getData()) {
//                        fragment.showList(response.body().getData().getList());
//                    } else {
//                        MToast.showToast(fragment.getActivity(), "暂无数据");
//                    }
//                }
//            }
//
//            @Override
//            public void onFailure(Call<BaseEntity<ProListEntity<List<AskLeaseSheBeiEntity>>>> call, Throwable t) {
//                Log.v("dd", "sdsdd");
//            }
//        });
//    }
//
//    public void getH5Url(String categoryType, String id) {
//        String urlName = "";
//        if ("1".equals(categoryType)) {//机械
//            urlName = "mechanicsRentDetails";
//        } else if ("2".equals(categoryType)) {//设备
//            urlName = "equipmentRentDetails";
//        } else if ("3".equals(categoryType)) {//车辆
//            urlName = "vehicleRentDetails";
//        } else if ("4".equals(categoryType)) {//房产
//            urlName = "houseRentDetails";
//        } else if ("5".equals(categoryType)) {//土地
//            urlName = "landRentDetails";
//        } else if ("6".equals(categoryType)) {//工厂
//            urlName = "factoryRentDetails";
//        } else if ("7".equals(categoryType)) {//农机
//            urlName = "farmRentDetails";
//        }
//        if (TextUtils.isEmpty(urlName)) {
//            return;
//        }
//        DialogHelper.showProgressDialog(fragment.getActivity(), "加载中...").setCanceledOnTouchOutside(false);
//        APIManager.getInstance().getPickAPI().getUrlSetting(urlName).enqueue(new Callback<BaseEntity<H5ReEntity>>() {
//            @Override
//            public void onResponse(Call<BaseEntity<H5ReEntity>> call, Response<BaseEntity<H5ReEntity>> response) {
//                DialogHelper.dismissProgressDialog();
//                if (response.body() == null || response.body().getState() == null) {
//                    return;
//                }
//                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
//                    String url = response.body().getData().getUrl() + "?id=" + id;
//                    WebViewX5HostActivity.start(fragment.getActivity(), url);
//                } else {
//                    MToast.showToast(fragment.getContext(), response.body().getMsg());
//                }
//            }
//
//            @Override
//            public void onFailure(Call<BaseEntity<H5ReEntity>> call, Throwable t) {
//                DialogHelper.dismissProgressDialog();
//            }
//        });
//    }
//
////    public void getCityData() {
////        APIManager.getInstance().getMachineAPI().getAreaSecondAndThird2(repertory.getAreaCode()).enqueue(new Callback<BaseEntity<List<BaseAreaEntity<List<BaseAreaEntity>>>>>() {
////            @Override
////            public void onResponse(Call<BaseEntity<List<BaseAreaEntity<List<BaseAreaEntity>>>>> call, Response<BaseEntity<List<BaseAreaEntity<List<BaseAreaEntity>>>>> response) {
////                if (response.body() == null || response.body().getState() == null) {
////                    return;
////                }
////
////                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
////                    askLeaseFragment.showAddressDatas(response.body().getData());
////                }
////            }
////
////            @Override
////            public void onFailure(Call<BaseEntity<List<BaseAreaEntity<List<BaseAreaEntity>>>>> call, Throwable t) {
////
////            }
////        });
////    }

    /**
     * 获取省市区地址
     */
    public void getCityData() {
        DialogHelper.showProgressDialog(fragment.getActivity(), "加载中...").setCanceledOnTouchOutside(false);
        AddressHelper.getInstance().getAllArea(new AddressHelper.AddressCallBack() {
            @Override
            public void initialData(List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> data) {
                DialogHelper.dismissProgressDialog();
                if (data != null && data.size() > 0) {
                    fragment.showAddressDatas(data,repertory.getAreaCode());
                }
            }
        });
    }
}
